package com.sophialex.health.record.common.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Event;

import com.sophialex.health.system.frame.util.RefMethod;



public class ListenerUtil {
	/** 只充许输入数字监听 */
	public final static KeyListener onlyNumberListener = new KeyListener() {
		public void keyReleased(KeyEvent e) {
		}

		public void keyPressed(KeyEvent e) {
			if ((e.character >= '0' && e.character <= '9') || e.character == 8) {
				e.doit = true;
			} else {
				e.doit = false;
			}
		}
	};
	
	// 在控件获得焦点，并按下键的时候触发
	public static final TraverseListener traverseListener = new TraverseListener() {
		public void keyTraversed(TraverseEvent e) {
			if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
				Event event = new Event();
				event.type = SWT.KeyDown;
				event.keyCode = SWT.TAB;
				e.display.post(event);
			}
		}
	};

	

	/**
	 * 根据value 值 与 com 当中的data 值比配 propeter 比对属性,不需要则填空
	 */
	public static void setComboValue(Combo com, Object value, String propeter) {
		if (com == null || com.isDisposed()) {
			return;
		}
		Object obj;
		if (value != null) {
			for (String s : com.getItems()) {
				obj = null;
				if (com.getData(s) != null) {
					if (propeter != null) {
						obj = RefMethod.invokeGetMethod(com.getData(s),
								RefMethod.gainGetMethod(propeter));
						if (obj != null
								&& value.toString().equals(obj.toString())) {
							com.setText(s);
							break;
						}
					} else if (value.toString().equals(
							com.getData(s).toString())) {
						com.setText(s);
						break;
					}
				}
			}
		}
	}

}
